package com.wisemoresystem.seal.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac_wktime;
import com.wisemoresystem.seal.model.t_mac_wktime_special;
import com.wisemoresystem.seal.util.SpringTools;

@Service
public class MacWktimeService extends BaseService {

	public List<Map<String, Object>> selectMacWktime(String bank_id){
		
		String  sql="select t.*,tb.name,tu.uname from t_mac_wktime t "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " left join t_user tu on t.user_id=tu._id "
				+ " where t.status=0 and t.bank_id=? order by t.created asc";
		
		List<Map<String, Object>> list = jt.queryForList(sql,new String[]{bank_id});
		return list;
	}
	
	public List<Map<String, Object>> selectNetMacWktime(String bank_id){
		
		String  sql="select t.* from t_mac_wktime t "
				+ " where t.status=0 and t.bank_id=? order by t.created asc";
		
		List<Map<String, Object>> list = jt.queryForList(sql,new String[]{bank_id});
		return list;
	}
	
	public List<Map<String, Object>> selectNetMacSpWktime(String bank_id){
		
		String  sql="select t.* from t_mac_wktime_special t "
				+ " where t.status=0 and t.bank_id=? order by t.created asc";
		
		List<Map<String, Object>> list = jt.queryForList(sql,new String[]{bank_id});
		return list;
	}
	
	public String selectUpBankId(String bank_id){
		
		String b_id = null;
		t_bank bank = this.findObjById(bank_id, t_bank.class);
		if(ChkTools.isNotNull(bank)){
			b_id = bank.getSuper_id();
		}
		
		return b_id;
	}
	
	public boolean macTime(String bank_id){
		String bank_id1 = bank_id;
		String bank_id2 = bank_id;
		List<Map<String, Object>> list = this.selectNetMacWktime(bank_id);
		while(ChkTools.isNull(list)){
			if(bank_id1=="root"||ChkTools.isNull(bank_id1)){
				break;
			}
			list = this.selectNetMacWktime(selectUpBankId(bank_id1));
			bank_id1 = selectUpBankId(bank_id1);
		
		}
		
		List<Map<String, Object>> list2 = this.selectNetMacSpWktime(bank_id);
		while(ChkTools.isNull(list2)){
			if(bank_id2=="root"||ChkTools.isNull(bank_id2)){
				break;
			}
			list2 = this.selectNetMacWktime(selectUpBankId(bank_id2));
			bank_id2 = selectUpBankId(bank_id2);
		}
		
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
		String  dt2 = df2.format(c.getTime());
		String date = df1.format(c.getTime());
		boolean flag = false;
		boolean flag1 = false;
		boolean flag2 = false;
		boolean flag3 = false;
		boolean flag4 = false;
		if(ChkTools.isNotNull(list)){
			for(Map<String,Object> w:list){
				t_mac_wktime wk = this.findObjById(w.get("_id").toString(), t_mac_wktime.class);
				if
				(wk.getStart_date().toString().compareTo(date)<=0&&wk.getEnd_date().toString().compareTo(date)>=0){
					if(wk.getStart_time().toString().compareTo(dt2)<=0&&wk.getEnd_time().toString().compareTo(dt2)>=0){
						flag1 = true;
						return flag1;
					}
				}
			}
		}else{
			flag1 = true;
			flag3 = true;
		}
		
		if(ChkTools.isNotNull(list2)){
			t_mac_wktime_special wks = this.findObjById(list2.get(0).get("_id").toString(), t_mac_wktime_special.class);
			flag2 = wktime_special(wks,dt2);
		}else{
			flag2 = true;
			flag4 = true;
		}
		
		if(ChkTools.isNotNull(list)&&ChkTools.isNotNull(list2)){
			if(flag1||flag2){
				flag = true;
			}
		}else{
			if(flag3&&flag4){
				flag = true;
			}else{
				if(flag3){
					flag = flag2;
				}else{
					flag = flag1;
				}
			}
		}
		
		return flag;
	}
	
	public boolean wktime_special(t_mac_wktime_special wks,String time){
		Calendar cal = Calendar.getInstance();
		int week  = cal.get(Calendar.DAY_OF_WEEK)-1;
		boolean flag = false; 
		if(week==1){
			if(wks.getWk1().toString().compareTo(time)<=0&&wks.getWk1e().toString().compareTo(time)>=0){
				flag = true;
			}
		}else if(week==2){
			if(wks.getWk2().toString().compareTo(time)<=0&&wks.getWk2e().toString().compareTo(time)>=0){
				flag = true;
			}
		}else if(week==3){
			if(wks.getWk3().toString().compareTo(time)<=0&&wks.getWk3e().toString().compareTo(time)>=0){
				flag = true;
			}
		}else if(week==4){
			if(wks.getWk4().toString().compareTo(time)<=0&&wks.getWk4e().toString().compareTo(time)>=0){
				flag = true;
			}
		}else if(week==5){
			if(wks.getWk5().toString().compareTo(time)<=0&&wks.getWk5e().toString().compareTo(time)>=0){
				flag = true;
			}
		}else if(week==6){
			if(wks.getWk6().toString().compareTo(time)<=0&&wks.getWk6e().toString().compareTo(time)>=0){
				flag = true;
			}
		}else if(week==7){
			if(wks.getWk7().toString().compareTo(time)<=0&&wks.getWk7e().toString().compareTo(time)>=0){
				flag = true;
			}
		}
		return flag;
	}
	
	public List<Map<String,Object>> wk_time(String bank_id){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();;
		String bank_id1 = bank_id;
		String bank_id2 = bank_id;
		List<Map<String, Object>> list1 = this.selectNetMacWktime(bank_id);
		while(ChkTools.isNull(list1)){
			list1 = this.selectNetMacWktime(selectUpBankId(bank_id1));
			bank_id1 = selectUpBankId(bank_id1);
			if(bank_id1=="root"||ChkTools.isNull(bank_id1)){
				break;
			}
		}
		
		List<Map<String, Object>> list2 = this.selectNetMacSpWktime(bank_id);
		while(ChkTools.isNull(list2)){
			list2 = this.selectNetMacWktime(selectUpBankId(bank_id2));
			bank_id2 = selectUpBankId(bank_id2);
			if(bank_id2=="root"||ChkTools.isNull(bank_id2)){
				break;
			}
		}
		
		if(ChkTools.isNotNull(list2)){
			list = list2;
		}
		if(ChkTools.isNotNull(list1)){
			for(Map<String,Object> m:list1){
				list.add(m);
			}
		}
		return list;
	}
	
	public static void main(String[] args) {
		MacWktimeService s = (MacWktimeService) SpringTools.getBean(MacWktimeService.class);
		System.out.println(s.macTime("4028814a4b25516c014b255ff9d40005"));
	}
}
